package com.xywy.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.util.StringUtils;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.Jtcy;
import com.xywy.me.bean.UserKeeper;
import com.xywy.start.RadarActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 我 - 家庭成员 - 添加
 * @author xiaogd
 *
 */
public class JtcyAddActivity extends BaseActivity{
    private static final String  TAG = "JtcyAddActivity";
    private Context   mContext;

    private EditText  name;
    private TextView  tvsex;
    private TextView  tvweight;
    private TextView  tvbirth;

    private Jtcy  jtcy;
    private TextView tvBack ;
    private TextView tvGo;
    @Override
    protected void onCreate(Bundle savedInstanceState)  
    {
    	super.onCreate(savedInstanceState);    
        //设置为全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_jtcy_add);

        this.preInit();
        this.initView();
        this.loadData();
        this.setListener();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    public void preInit() {
        mContext  =this.context;
        Intent  intent = getIntent();
        jtcy  = (Jtcy)intent.getSerializableExtra("jtcy");
    }

    @Override
    public void loadData() {

    }

    @Override
    public void initView() {
        //title
        TitleBar2 titleBar = (TitleBar2)findViewById(R.id.me_jtcy_add_title_bar);
        titleBar.onClickBack(this);
            TextView tvTitle= titleBar.getTitle();
             tvBack= titleBar.getTvBack();
             tvGo= titleBar.getGo();

            tvTitle.setText(getString(R.string.me_jtcy));
            tvBack.setText(getString(R.string.me_jtcy_add_back));
            tvGo.setText(getString(R.string.me_jtcy_add_go));

        name = (EditText) findViewById(R.id.et_me_jtcy_add_name);
        tvsex =  (TextView)findViewById(R.id.tv_me_jtcy_add_sex);
        tvweight =  (TextView)findViewById(R.id.tv_me_jtcy_add_height);
        tvbirth =  (TextView)findViewById(R.id.tv_me_jtcy_add_birth);
    }

    public void  onClcikMeAddInfor(View view){
        int   target =0;
        if(StringUtils.isNull(name.getText().toString())){
            Toast.makeText(mContext,getString(R.string.me_jtcy_name_not_null),Toast.LENGTH_LONG).show();
        }else{
            if(jtcy==null){
                 jtcy =new Jtcy();
            }
            jtcy.setAccountstr(name.getText().toString());

            switch (view.getId()){
                case R.id.rl_me_jtcy_add_sex:
                    target = ConstantValue.SEX;
                    break;
                case R.id.rl_me_jtcy_add_height:
                    target = ConstantValue.HEIGHT;
                    break;
                case R.id.rl_me_jtcy_add_birth:
                    target = ConstantValue.BIRTH;
                    break;
            }
            Intent  intent = new Intent(mContext, JtcyAddUpdateActivity.class);
            intent.putExtra("item",target);
            intent.putExtra("jtcy",jtcy);
            startActivity(intent);
        }
    }


    MyProgress myProgress;
    JSONObject msgBack=null;
    /**
     * 返回
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
            myProgress.stop();
            if(null!=msg.obj){
                try {
                    msgBack = new JSONObject(msg.obj.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if(msg.arg1== ResultCode.SUCCESS){
                    Intent intent =new Intent(mContext, FragmentMainActivity.class);
                    mContext.startActivity(intent);
                    finish();
                }else{
                    Toast.makeText(mContext, msgBack.optString("error"), Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    //封装Parser参数
    private Parser paramsParser;
    //封装httpclient参数
    private HashMap<String, Object> params;
    //异步加载
    private AsyncHttpClient asyncHttpClient;

    //处理网络请求
    public void sendMsg( ) {
        myProgress = new MyProgress(this,10);
        myProgress.start();
        paramsParser = new Parser();

        //封装参数
        params = new HashMap<String, Object>();

        //请求路径
        paramsParser.action = getString(R.string.add_child_account);
        //请求方式
        paramsParser.request = "post";

        SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        String userid = UserKeeper.getCurrentUser(this).getUserid();

      //请求参数
        params.put("userid",userid);
        params.put("accountstr",name.getText().toString());
        params.put("sex",jtcy.getSex());
        params.put("height",jtcy.getHeight());
        params.put("birthday",System.currentTimeMillis());
        paramsParser.map = params;
        //传递上下文
        paramsParser.context = this.context;

        asyncHttpClient = AsyncHttpClient.getInstance();
        asyncHttpClient.exeute(paramsParser,onResultListenerAdapter);
    }

    OnResultListenerAdapter onResultListenerAdapter =new OnResultListenerAdapter(){
        @Override
        public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
            super.onSuccess(resultParser, resultCode);
            Message message = Message.obtain();
            message.arg1 = resultCode;
            message.obj = resultParser.back;
            handler.sendMessage(message);
        }
    };

    private void setListener(){
         tvBack.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {

             }
         });

        tvGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(mContext, RadarActivity.class);
                mContext.startActivity(intent);
            }
        });
    }
}



